package com.yhsimon.realm.member.controller;

import com.yhsimon.realm.member.dto.index.LoginDto;
import com.yhsimon.realm.member.service.UserService;
import com.yhsimon.realm.member.service.ValidateCodeService;
import com.yhsimon.realm.member.utils.AuthContextUtils;
import com.yhsimon.realm.member.utils.R;
import com.yhsimon.realm.member.vo.index.LoginVo;
import com.yhsimon.realm.member.vo.index.ValidateCodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/member/index")
public class IndexController {

    @Autowired
    private UserService userService;

    @Autowired
    private ValidateCodeService validateCodeService;

    @PostMapping("/login")
    public R login(@RequestBody LoginDto loginDTO) {
        LoginVo loginVo = userService.login(loginDTO);
        return R.ok().put("data", loginVo);
    }


    @GetMapping(value = "/generateValidateCode")
    public R generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return R.ok().put("data", validateCodeVo);
    }

    @GetMapping(value = "/getUserInfo")
    public R getUserInfo() {
        // 直接从threadLocal中获取当前用户信息
        return R.ok().put("data", AuthContextUtils.getUserInfo());
    }

    @GetMapping(value = "/logout")
    public R logout(@RequestHeader(value = "token") String token) {
        userService.logout(token);
        return R.ok();
    }


}
